/**
  * Copyright (c) CSIRO Australia, 2009
  *
  * @author $Author: jia020 $
  * @version $Id: TasksPanel.java 643 2009-05-20 00:20:55Z jia020 $
  */
package au.csiro.nt.pdsp.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListener;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;

/**
 * Class to create TasksPanel for tasks processing.
 * 
 */
public class TasksPanel extends Panel {
	private EventHandler eventHandler = null;
	private final PDQueryServiceAsync service = GWT.create(PDQueryService.class);	
	private DynamicGrid grid = null;
	/**
	 * Constructor for TasksPanel
	 * @param eventHandler for callback.
	 * @param xmlData xml formated tasks data.
	 */
	public TasksPanel(final EventHandler eventHandler,String xmlData) {
		super();
		this.eventHandler = eventHandler;
		this.setBorder(false);
		this.setPaddings(5);
		this.setLayout(new FitLayout());
		grid = new DynamicGrid(xmlData);		
		grid.addGridRowListener(new GridRowListener() {  
			@Override
			public void onRowClick(GridPanel grid, int rowIndex, EventObject e) {
		
			}

			@Override
			public void onRowContextMenu(GridPanel grid, int rowIndex, EventObject e) {
	
			}

			@Override
			public void onRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
				showWorkflowsPanel();		
			}  
		});  
		Toolbar toolbar = new Toolbar();  
		ToolbarButton button = new ToolbarButton("Retrieve detail data", new ButtonListenerAdapter() {  
		    public void onClick(Button button, EventObject e) {  
					showWorkflowsPanel();	
		    }  
		});  
		toolbar.addButton(button);  
		grid.setBottomToolbar(toolbar);  		
		this.add(grid);
	}
	
	private void showWorkflowsPanel() {
		Record rc = grid.getSelectionModel().getSelected();
		int taskid = rc.getAsInteger("taskid");
		service.getMyWorkflows(taskid, new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
//          if(caught instanceof AuthenticationException) {
//                //redirecttoLogin();
//          }
//          else {
//          	 MessageBox.alert("failed on query");
//          }
       }					  
				public void onSuccess(String result) {
					eventHandler.processEvent(PdspConst.EVENT_SHOW_MYWORKFLOWS,result);
			}
		});							
	}

}
